package com.lanou.controller;

import com.lanou.bean.Account;
import com.lanou.mapper.AccountMapper;
import com.lanou.service.AccountService;
import com.lanou.utils.AjaxResult;
import com.lanou.service.ServiceService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 18/3/15.
 */
@Controller
public class AccountController {
    @Resource
    AccountService accountService;
    @Resource
    ServiceService serviceService;

    @RequestMapping(value = "/account_list")
    public String account_list(){
        if (SecurityUtils.getSubject().isPermitted("账务账号")){
            return "account/account_list";
        }else {
            return "nopower";
        }
    }
    @RequestMapping(value = "/account_add")
    public String account_add(){
        return "account/account_add";
    }
    @RequestMapping(value = "/account_detail")
    public String account_detail(){
        return "account/account_detail";
    }
    @RequestMapping(value = "/account_modi")
    public String account_modi(){
        return "account/account_modi";
    }

    /**
     * 查询所有账务信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllAccountData")
    public List<Account> getAllAccountData() {
        return accountService.getAllAccountInfo();
    }

    @RequestMapping(value = "/delAccountData")
    public String deleAccountData(@RequestParam("id") int id) {
        accountService.deleteAccount(id);
        serviceService.deleteServiceByAccount(id);
        return "account/account_list";
    }

    /**
     * 设置状态
     * @param id 账务账号Id
     * @param status 状态
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/AccountstarOrStop")
    public AjaxResult starOrStop(@RequestParam("id") int id, @RequestParam("status") String status) {
        if (status.equals("0")) {
            status = "1";
            accountService.updateAccount(id, status);
            accountService.updateDelPauseDate(null,id);
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.setMsg("/account_list");
            return ajaxResult;
        } else if (status.equals("1")){
            status = "0";
            accountService.updateAccount(id, status);
            accountService.updatePauseDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(System.currentTimeMillis())),id);
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.setMsg("/account_list");
            return ajaxResult;
        }else {
            status = "2";
            accountService.updateAccount(id, status);
            accountService.updateCloseDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(System.currentTimeMillis())),id);
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.setMsg("/account_list");
            return ajaxResult;
        }

    }

    /**
     * 添加Account
     * @param realName
     * @param idcardNo
     * @param loginName
     * @param loginPasswd
     * @param loginPasswd1
     * @param telephone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addAccount")
    public AjaxResult addAccount(@RequestParam("realName") String realName,
                                 @RequestParam("idcardNo") String idcardNo,
                                 @RequestParam("loginName") String loginName,
                                 @RequestParam("loginPasswd") String loginPasswd,
                                 @RequestParam("loginPasswd1") String loginPasswd1,
                                 @RequestParam("telephone") String telephone) {
        Account account = new Account();
        account.setRealName(realName);
        account.setIdcardNo(idcardNo);
        account.setLoginName(loginName);
        account.setLoginPasswd(loginPasswd);
        account.setTelephone(telephone);
        account.setStatus("1");
        account.setCreateDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(System.currentTimeMillis())));
        accountService.addAccount(account);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg("/account_list");
        return ajaxResult;
    }

    /**
     * 通过账务账号查询账务信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectAccountById")
    public Account selectAccountById(@RequestParam("accountId") int id){

       return accountService.selectAccountById(id);
    }

    /**
     * 根据账务账号Id修改账务信息
     * @param id
     * @param name
     * @param tele
     * @param cardno
     * @param email
     * @param addr
     * @param zipcode
     * @param qq
     * @param gender
     * @param occupation
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateAccountById")
    public AjaxResult updateAccountById(@RequestParam("id") int id,@RequestParam("realname")String name,
                                        @RequestParam("tele") String tele,@RequestParam("cardno")String cardno,
                                        @RequestParam("email")String email,@RequestParam("addr") String addr,
                                        @RequestParam("zipcode") String zipcode,@RequestParam("qq") String qq,
                                        @RequestParam("gender") String gender,@RequestParam("occupation")String occupation){

        accountService.updateAccountByid(id, name, tele, cardno, email, addr, zipcode, qq, gender,occupation);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg("/account_list");
        return ajaxResult;
    }




}
